package com.pj.wisdomlogistics.ui.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.RepairPeopleInfo;
import com.pj.wisdomlogistics.mvp.contract.RepairPoritionContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.RepairPoritionPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.RepairPortionAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RepairPoritionActivity extends MvpActivity<RepairPoritionPresenter> implements RepairPoritionContract.View, RepairPortionAdapter.listener {
    @BindView(R.id.rv_portion)
    RecyclerView mRvPortion;

    private RepairPortionAdapter mAdapter;
    private int mSelectedPos = -1;

    private List<RepairPeopleInfo> mInfos;
    private RepairPeopleInfo mInfo;


    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        mRvPortion.setLayoutManager(new GridLayoutManager(this , 2 , LinearLayoutManager.VERTICAL , false));
        mAdapter = new RepairPortionAdapter(this);
        mRvPortion.setAdapter(mAdapter);

        mAdapter.setListener(this);

        mPresenter.findFixWorker();
        showProgressDialog();

    }



    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_repair_porition;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick({R.id.iv_back , R.id.tv_sure})
    public void onClick(View mView){
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_sure:
                if(mInfo == null){
                    CustomToast.showToast(this ,"请选择维修人员" , 1000);
                    return;
                }
                EventBus.getDefault().post(new EventCenter<>(Cons.RepairInfo.REPAIR_PORITION_OK , mInfo));
                ActivityManagerUtil.create().finishActivity();
                break;
        }
    }


    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this ,errorMsg , 1000);
    }

    @Override
    public void onSuccess(List<RepairPeopleInfo> mInfo) {
        dismissProgressDialog();
        this.mInfos = mInfo;
        mAdapter.setList(mInfos);
    }


    @Override
    public void itemClick(RepairPeopleInfo mInfo , int position) {
        this.mInfo = mInfo;

        BaseViewHolder vh;
        if(mSelectedPos == -1){
            vh = (BaseViewHolder) mRvPortion.findViewHolderForLayoutPosition(position);
        }else {
            vh = (BaseViewHolder) mRvPortion.findViewHolderForLayoutPosition(mSelectedPos);
        }

        if(vh != null){
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
        }else{
            mAdapter.notifyItemChanged(mSelectedPos);
        }

        if(mSelectedPos == -1){
            mSelectedPos = position;
            mInfos.get(mSelectedPos).setSelected(true);
        }else{
            mInfos.get(mSelectedPos).setSelected(false);
            mSelectedPos = position;
            mInfos.get(mSelectedPos).setSelected(true);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.portion_bg_normal);
            vh = (BaseViewHolder) mRvPortion.findViewHolderForLayoutPosition(mSelectedPos);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
        }
    }
}
